mybatis之DAO层自动实现接口
* 使用mybatis举例,使用DAO接口方式实现
* 不需要针对接口去编写具体的实现类代码,编写映射XML文件,直接拿来使用即可。
* 1、导入jar包:mybatis和mysql-connector
* 2、mybatis配置文件:mybatis-config.xml,加载映射XML文件
* 3、编写JavaBean类,如UserBean
* 4、编写DAO接口和映射XML文件(namespace=DAO接口文件路径、id=接口方法名)
mybatis-config-dao.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <!--从外部配置文件导入jdbc信息--> 7 <properties resource="jdbc.properties"></properties> 8 9 <environments default="development"> 10 <environment id="development"> 11 <transactionManager type="JDBC"/> 12 <dataSource type="POOLED"> 13 <property name="driver" value="${driver}"/> 14 <property name="url" value="${url}"/> 15 <property name="username" value="${username}"/> 16 <property name="password" value="${password}"/> 17 </dataSource> 18 </environment> 19 </environments> 20 21 <!--指定映射资源文件--> 22 <mappers> 23 <mapper resource="dao/DaoMapper.xml"/> 24 25 </mappers> 26 </configuration>
DaoMapper.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 6 <mapper namespace="dao.UserDao"> 7 <!--id:名称; parameterType传入参数类型; resultType:返回数据类型--> 8 <select id="selectOneUser" parameterType="int" resultType="bean.User"> 9 select * from user where id = #{id} 10 </select> 11 12 <!--批量查询--> 13 <select id="selectAllUser" resultMap="userList"> 14 select * from user 15 </select> 16 <resultMap type="bean.User" id="userList"> 17 <result property="id" column="id"/> 18 <result property="name" column="name"/> 19 <result property="age" column="age"/> 20 </resultMap> 21 22 <insert id="insertUser" useGeneratedKeys="true" keyProperty="id"> 23 insert into user (id, name, age) values (#{id} ,#{name}, #{age}) 24 </insert> 25 26 <update id="updateUser"> 27 update user set name=#{name},age=#{age} where id=#{id} 28 </update> 29 30 <delete id="deleteUser"> 31 delete from user where id=#{id} 32 </delete> 33 34 <!-- 传入多个参数时,自动转换为map形式 --> 35 <insert id="insertByColumns" useGeneratedKeys="true" keyProperty="id"> 36 insert into user (id, name, age) values (NULL ,#{param1}, #{param2}) 37 </insert> 38 <insert id="insertByZhuJie" useGeneratedKeys="true" keyProperty="id"> 39 insert into user (id, name, age) values (NULL ,#{name}, #{age}) 40 </insert> 41 <insert id="insertByMap" useGeneratedKeys="true" keyProperty="id"> 42 insert into user (id, name, age) values (NULL ,#{name}, #{age}) 43 </insert> 44 45 </mapper>
UserDao.java
1 package dao; 2 3 import bean.User; 4 import org.apache.ibatis.annotations.Param; 5 6 7 import java.util.List; 8 import java.util.Map; 9 10 /* 11 * 使用mybatis举例,使用DAO接口方式实现 12 * 不需要针对UserMapperI接口去编写具体的实现类代码,这个具体的实现类由MyBatis帮我们动态构建出来,我们只需要直接拿来使用即可。 13 * */ 14 public interface UserDao { 15 16 // 根据id查询单一数据 17 public User selectOneUser(int id); 18 19 // 查询所有用户 20 public List<User> selectAllUser(); 21 22 // 插入数据 23 public int insertUser(User user); 24 25 // 修改用户 26 public int updateUser(User user); 27 28 // 删除用户 29 public int deleteUser(User user); 30 31 // 插入数据;多参数三种方式:默认Map、使用Map传参和使用注解别名 32 public int insertByColumns(String name, int age); 33 public int insertByZhuJie(@Param("name") String name, @Param("age") int age); 34 public int insertByMap(Map map); 35 36 }
TestDao.java
1 package dao; 2 3 import bean.User; 4 import org.apache.ibatis.io.Resources; 5 import org.apache.ibatis.session.SqlSession; 6 import org.apache.ibatis.session.SqlSessionFactory; 7 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 8 import org.junit.After; 9 import org.junit.Before; 10 import org.junit.Ignore; 11 import org.junit.Test; 12 13 import java.io.IOException; 14 import java.io.InputStream; 15 import java.util.HashMap; 16 import java.util.List; 17 import java.util.Map; 18 19 /* 20 * 使用mybatis举例,使用DAO接口方式实现 21 * 不需要针对接口去编写具体的实现类代码,编写映射XML文件,直接拿来使用即可。 22 * 1、导入jar包:mybatis和mysql-connector 23 * 2、mybatis配置文件:mybatis-config.xml,加载映射XML文件 24 * 3、编写JavaBean类,如UserBean 25 * 4、编写DAO接口和映射XML文件(namespace=DAO接口文件路径、id=接口方法名) 26 * */ 27 public class TestDao { 28 String resource = "mybatis-config-dao.xml"; 29 SqlSessionFactory sqlSessionFactory = null; 30 SqlSession session = null; 31 32 @Before 33 public void before() { 34 // System.out.println("Before"); 35 try { 36 InputStream inputStream = Resources.getResourceAsStream(resource); 37 // 创建工厂 38 sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 39 // 创建session对象 40 session = sqlSessionFactory.openSession(); 41 42 } catch (IOException e) { 43 e.printStackTrace(); 44 } 45 } 46 47 @After 48 public void close() { 49 session.close(); 50 // System.out.println("After"); 51 } 52 53 @Test 54 public void testSelectOneUser() { 55 // 接口自动实例化 56 UserDao userDao = session.getMapper(UserDao.class); 57 // 执行sql 58 User user = userDao.selectOneUser(1); 59 System.out.println(user); 60 } 61 62 // 批量查询 63 @Test 64 public void testSelectAllUser() { 65 // 接口自动实例化 66 UserDao userDao = session.getMapper(UserDao.class); 67 // 执行sql 68 List<User> listUser = userDao.selectAllUser(); 69 System.out.println("记录个数:" + listUser.size()); 70 System.out.println(listUser); 71 } 72 73 // 插入数据 74 @Ignore 75 @Test 76 public void testInsertUser() { 77 UserDao userDao = session.getMapper(UserDao.class); 78 User user = new User("AA", "29"); 79 int n = userDao.insertUser(user); 80 session.commit(); 81 System.out.println("插入数据:" + user); 82 System.out.println(n); 83 } 84 85 // 修改数据 86 @Ignore 87 @Test 88 public void testUpdateUser() { 89 UserDao userDao = session.getMapper(UserDao.class); 90 User user = new User(2, "Tom5", "29"); 91 int n = userDao.updateUser(user); 92 session.commit(); 93 System.out.println("修改数据:" + user); 94 System.out.println(n); 95 } 96 97 // 修改数据 98 @Ignore 99 @Test 100 public void testDeleteUser() { 101 UserDao userDao = session.getMapper(UserDao.class); 102 User user = new User(13, "XX", "XX"); 103 int n = userDao.deleteUser(user); 104 session.commit(); 105 System.out.println("删除数据:" + user); 106 System.out.println(n); 107 } 108 109 // 多参数插入数据的三种方式 110 @Ignore 111 @Test 112 public void testInsertByColumns() { 113 UserDao userDao = session.getMapper(UserDao.class); 114 String name = "insertByColumns"; 115 int age = 33; 116 // int n = userDao.insertByColumns(name, age); 117 // int n = userDao.insertByZhuJie(name, age); 118 119 Map map = new HashMap(); 120 map.put("name", name); 121 map.put("age", age); 122 int n = userDao.insertByMap(map); 123 124 session.commit(); 125 System.out.println("插入数据name:" + name); 126 System.out.println(n); 127 } 128 129 130 }
User.java
1 package bean; 2 3 /** 4 * 数据封装类 5 * JavaBean 6 */ 7 public class User { 8 private int id; 9 private String name; 10 private String age; 11 12 public User() { 13 14 } 15 16 public User(String name, String age) { 17 this.name = name; 18 this.age = age; 19 } 20 public User(int id, String name, String age) { 21 this.id = id; 22 this.name = name; 23 this.age = age; 24 } 25 26 public String getName() { 27 return name; 28 } 29 30 public void setName(String name) { 31 this.name = name; 32 } 33 34 public String getAge() { 35 return age; 36 } 37 38 public void setAge(String age) { 39 this.age = age; 40 } 41 42 public int getId() { 43 return id; 44 } 45 46 public void setId(int id) { 47 this.id = id; 48 } 49 50 @Override 51 public String toString() { 52 return "User{id:" + id + "; 姓名:" + name + "; 年龄:" + age + "}"; 53 } 54 }